package org.leafDomain.common;

import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;



public interface Com {
    Logger loggerCom = Logger.getLogger(Com.class);

    // 获取配置对象
    Properties config();

    // 释放资源
    void close();

    // 初始化资源
    void init() throws Exception;


    /**
     * hbase列族和列与 列 的映射关系
     *             hbase列族     列     列值
     * @return Map<String, Map<String, String>>
     */
    default Map<String, Map<String, String>> hBaseColumnsMapping(String configItem){
        final String mapping = checkAndGetConfig(configItem);
        Map<String, Map<String, String>> map = new HashMap<>();
        final String[] pss = mapping.split(",");
        for (String ps : pss) {
            final String[] pp = ps.split("->");
            final String[] p = pp[0].split(":");
            String col = pp[1], hbaseColFamily, hbaseColName;
            if(p.length == 1){
                map.put("ROWKEY", new HashMap<>());
                map.get("ROWKEY").put("ROWKEY", col);
            }else {
                hbaseColFamily = p[0];
                hbaseColName = p[1];
                if(!map.containsKey(hbaseColFamily)){
                    map.put(hbaseColFamily, new HashMap<>());
                }
                map.get(hbaseColFamily).put(hbaseColName, col);
            }
        }
        return map;
    }


    default byte[] toBytes(Object obj){
        if(obj instanceof String){
            return Bytes.toBytes((String) obj);
        }else if(obj instanceof Float){
            return Bytes.toBytes((Float) obj);
        }else if(obj instanceof Double){
            return Bytes.toBytes((Double) obj);
        }else if(obj instanceof BigDecimal){
            return Bytes.toBytes(((BigDecimal) obj));
        }else if(obj instanceof Short){
            return Bytes.toBytes((Short) obj);
        }else if(obj instanceof Integer){
            return Bytes.toBytes((Integer) obj);
        }else if(obj instanceof Boolean){
            return Bytes.toBytes((Boolean) obj);
        }else{
            throw new RuntimeException("HBase不支持转化为字节数组的类型："+obj.getClass().getName());
        }
    }


    // 检查配置参数
    default String checkAndGetConfig(String key){
        if(!config().containsKey(key)){
            throw new RuntimeException("配置项缺失异常：" + key);
        }
        final String item = config().getProperty(key);
        loggerCom.info(String.format("获取配置项%s : %s", key, item));
        return item;
    }
    // 统一资源释放
    default void closeAll(AutoCloseable...acs){
        for (AutoCloseable ac : acs) {
            if(Objects.nonNull(ac)){
                try {
                    ac.close();
                    loggerCom.info(String.format("释放 %s 成功", ac.getClass().getName()));
                } catch (Exception e) {
                    loggerCom.error("释放资源异常", e);
                }
            }
        }
    }
}
